package com.hex.ds.hdrs.period.job.controller;


import com.hex.bigdata.hbda.annotation.Log;
import com.hex.bigdata.hbda.base.Page;
import com.hex.ds.hdrs.common.page.PageData;
import com.hex.ds.hdrs.engine.service.GenerateService;
import com.hex.ds.hdrs.period.job.converter.IPeriodJobDefVoConverter;
import com.hex.ds.hdrs.period.job.dto.PeriodJobDefDto;
import com.hex.ds.hdrs.period.job.po.PeriodJobDef;
import com.hex.ds.hdrs.period.job.service.PeriodJobDefService;
import com.hex.ds.hdrs.period.job.vo.PeriodJobDefVo;
import com.hex.ds.hdrs.period.job.vo.query.PeriodJobDefQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 周期作业定义表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-11-26
 */
@Slf4j
@Api("周期作业定义控制器")
@RestController
@RequestMapping("/api/jobDef")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PeriodJobDefController {

    private final PeriodJobDefService periodJobDefService;

    private final GenerateService generateService;

    private final IPeriodJobDefVoConverter converter;

    /*
     * @Method: queryListByPage <br>
     * @Param: [periodJobDefQueryVo, page] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：周期作业定义-分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/5/5 15:04 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping
    @ApiOperation("周期作业定义-分页查询")
    public ResponseEntity<Object> queryListByPage(PeriodJobDefQueryVo periodJobDefQueryVo, Page page) {
        log.debug("【PeriodJobDefController.queryListByPage】参数：{}", periodJobDefQueryVo.toString());
        PeriodJobDefDto periodJobDefDto = converter.toPeriodJobDefDto(periodJobDefQueryVo);
        return new ResponseEntity<>(periodJobDefService.queryListByPage(periodJobDefDto, page), HttpStatus.OK);
    }

    /*
     * @Method: addJobDef <br>
     * @Param: [periodJobDefVo] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：新增周期作业定义<br>
     * @Author： wz.li<br>
     * @Date： 2023/3/29 19:41 <br>
     * @Version： V2.0.2<br>
     */
    @PostMapping
    @Log("新增周期作业定义")
    @ApiOperation("新增周期作业定义")
    public ResponseEntity<Object> add(@RequestBody PeriodJobDefVo periodJobDefVo) {
        log.info("【PeriodJobDefController.add】新增周期作业定义，参数：{}", periodJobDefVo.toString());
        PeriodJobDef jobDef = periodJobDefService.add(converter.toPeriodJobDefDto(periodJobDefVo));
        return new ResponseEntity<>(jobDef.getPkId(), HttpStatus.OK);
    }

    /**
     * 通过name与code获取所有作业
     *
     * @return
     */
    @Log("通过name与code获取所有作业")
    @ApiOperation("通过name与code获取所有作业")
    @GetMapping(value = "/getJobList")
    public ResponseEntity<Object> getJobList(PeriodJobDef jobDef, Page page) {
        log.debug("查询作业定义信息【PeriodJobDefController.qryJobDefPage】========》{}", jobDef);
        PageData data = new PageData(this.periodJobDefService.getJobList(jobDef, page), page);
        return new ResponseEntity<>(data, HttpStatus.OK);
    }


    /**
     * 通过jobCode查询jobdef对象
     *
     * @param jobCode
     * @return JobDef 信息
     */
    @Log("通过jobCode查询jobdef信息")
    @ApiOperation("通过jobCode查询jobdef信息")
    @GetMapping(value = "/qryJobDefByJobCode")
    public ResponseEntity<Object> qryJobDefByJobCode(@RequestParam String jobCode) {
        log.debug("查询 jobdef 信息【PeriodJobDefController.qryJobDefdebug】========》{}", jobCode);
        PeriodJobDef jobdef = this.periodJobDefService.getJobDefByJobCode(jobCode);
        return new ResponseEntity<>(jobdef, HttpStatus.OK);
    }

    /**
     * 通过jobCode 查询jobDef是否已经存在
     *
     * @param
     * @return
     */
    @PostMapping("/checkAppData")
    @Log("新增作业Code是否重复")
    @ApiOperation("新增作业Code是否重复")
    public ResponseEntity<Object> checkAppData(@RequestBody PeriodJobDef periodJobDef) {
        log.debug("查询 jobDef 信息 是否存在【PeriodJobDefController.isNameExit】========》{}", periodJobDef);
        boolean res = this.periodJobDefService.checkAppData(periodJobDef);
        return new ResponseEntity<>(res, HttpStatus.OK);
    }


    /*
     * @Method: update <br>
     * @Param: [periodJobDefVo] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：更新周期作业定义<br>
     * @Author： wz.li<br>
     * @Date： 2023/3/29 19:42 <br>
     * @Version： V2.0.2<br>
     */
    @PutMapping
    @Log("更新周期作业定义")
    @ApiOperation("更新周期作业定义")
    public ResponseEntity<Object> update(@RequestBody PeriodJobDefVo periodJobDefVo) {
        log.info("【PeriodJobDefController.update】更新周期作业定义，参数：{}", periodJobDefVo.toString());
        periodJobDefService.modifyById(converter.toPeriodJobDefDto(periodJobDefVo));
        return new ResponseEntity<>("修改成功", HttpStatus.OK);
    }


    /**
     * 删除 jobDef 信息
     *
     * @param pkIds
     * @return
     */
    @DeleteMapping
    @Log("删除jobDef信息/批量")
    @ApiOperation("删除jobDef信息/批量")
    public ResponseEntity<Object> delJobDef(@RequestBody List<String> pkIds) {
        log.debug("删除作业【PeriodJobDefController.isExit】========》{}", pkIds);
        this.periodJobDefService.batchRemoveById(pkIds);
        return new ResponseEntity<>("删除成功！", HttpStatus.OK);
    }

    @Log("批量更新作业状态")
    @ApiOperation("批量更新作业状态")
    @PostMapping("/batchUpdateStatus")
    public ResponseEntity<Object> batchUpdateStatus(@RequestBody PeriodJobDefDto periodJobDefDto) {
        log.debug("批量更新作业状态【PeriodJobDefController.batchUpdateStatus】参数periodJobDefDto: {}", periodJobDefDto.toString());
        this.periodJobDefService.batchUpdateStatus(periodJobDefDto);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Log("批量更新作业调度间隔")
    @ApiOperation("批量更新作业调度间隔")
    @PostMapping("/batchUpdateJobPeriod")
    public ResponseEntity<Object> batchUpdateJobPeriod(@RequestBody PeriodJobDefDto periodJobDefDto) {
        log.debug("批量更新作业调度间隔【PeriodJobDefController.batchUpdateJobPeriod】参数periodJobDefDto: {}", periodJobDefDto.toString());
        this.periodJobDefService.batchUpdateJobPeriod(periodJobDefDto);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Log("批量更新作业偏移小时")
    @ApiOperation("批量更新作业偏移小时")
    @PostMapping("/batchUpdateOffsetHour")
    public ResponseEntity<Object> batchUpdateOffsetHour(@RequestBody PeriodJobDefDto periodJobDefDto) {
        log.debug("批量更新作业偏移小时【PeriodJobDefController.batchUpdateOffsetHour】参数periodJobDefDto: {}", periodJobDefDto.toString());
        this.periodJobDefService.batchUpdateOffsetHour(periodJobDefDto);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }


    @Log("批量更新作业优先级")
    @ApiOperation("批量更新作业优先级")
    @PostMapping("/batchUpdateSortNo")
    public ResponseEntity<Object> batchUpdateSortNo(@RequestBody PeriodJobDefDto periodJobDefDto) {
        log.debug("批量更新作业优先级【PeriodJobDefController.batchUpdateOffsetHour】参数periodJobDefDto: {}", periodJobDefDto.toString());
        this.periodJobDefService.batchUpdateSortNo(periodJobDefDto);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Log("批量更新作业生效日期")
    @ApiOperation("批量更新作业生效日期")
    @PostMapping("/batchUpdateDateRange")
    public ResponseEntity<Object> batchUpdateDateRange(@RequestBody PeriodJobDefDto periodJobDefDto) {
        log.debug("批量更新作业生效日期【PeriodJobDefController.batchUpdateDateRange】参数periodJobDefDto: {}", periodJobDefDto.toString());
        this.periodJobDefService.batchUpdateDateRange(periodJobDefDto);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Log("初始化作业和任务信息")
    @ApiOperation("初始化作业和任务信息")
    @GetMapping("/initJobAndTaskInfo")
    public ResponseEntity<Object> initJobAndTaskInfo() {
        log.debug("初始化作业和任务信息【PeriodJobDefController.initJobAndTaskInfo】");
        return new ResponseEntity<>(this.periodJobDefService.initJobAndTaskInfo(), HttpStatus.OK);
    }

    @Log("获取当前应用下所有作业定义")
    @ApiOperation("获取当前应用下所有作业定义")
    @PostMapping("/getJobListByCurrentApp")
    public ResponseEntity<Object> getJobListByCurrentApp(@RequestBody Map map) {
        log.debug("初始化作业和任务信息【PeriodJobDefController.getJobListByCurrentApp】");
        PageData pageData = this.periodJobDefService.getDependJobByCurrentApp(map);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }

    @Log("导入周期作业任务")
    @ApiOperation("导入周期作业任务")
    @PostMapping("/upload")
    public ResponseEntity<Object> uploadTaskDef(HttpServletRequest request) {
        log.info("导入周期作业任务【PeriodJobDefController.uploadTaskDef】");
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map resultMap = periodJobDefService.importExcel(multipartRequest.getFile("file"),
                request.getParameter("batchTag"), request.getParameter("appCode"));
        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }

    @Log("导出周期作业任务")
    @ApiOperation("导出周期作业任务")
    @PostMapping(value = "/download")
    public ResponseEntity<Object> downloadJobDef(@RequestBody PeriodJobDef periodJobDef) throws IOException {
        log.info("导出周期作业任务【PeriodJobDefController.downloadJobDef】");
        String filePath = periodJobDefService.exportExcel(periodJobDef);
        byte[] bytes = Files.readAllBytes(new File(filePath).toPath());
        ByteArrayResource bar = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=test2.zip")
                .body(bar);
    }

    @Log("生成任务实例")
    @ApiOperation("生成任务实例")
    @PostMapping("/generateTaskInstance")
    public ResponseEntity<Object> generateTaskInstance() {
        log.info("生成任务实例【PeriodJobDefController.generateTaskInstance】");
        generateService.generateTaskInstance();
        return new ResponseEntity<>(true, HttpStatus.OK);
    }
}

